/*
 * Copyright 2012 Fabian Wenzelmann
 * 
 * This file is part of Recognition-Procedures-for-Boolean-Functions.
 * 
 * Recognition-Procedures-for-Boolean-Functions is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Recognition-Procedures-for-Boolean-Functions is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Recognition-Procedures-for-Boolean-Functions.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.wenzelmf.booleanFunction.booleanExpression;

import java.util.Iterator;

/**
 * Superclass for all Boolean function implementations.
 * 
 * A boolean function takes an Boolean input point and returns 0 or 1.
 * The point can be represented by an Iterator of type Boolean (the iteration
 * consists of all required input fields), an array of boolean values or an
 * arbitrary object that implements the Iterable interface.
 * <br>
 * You only have to implement the method {@link #getValue(Iterator)}, all other
 * methods are implemented in this class and use this method.
 * 
 * @author Fabian Wenzelmann
 * @version 1.0
 *
 */
public abstract class BooleanFunction
{
	/**
	 * Return the value of the Boolean function given the input point as an Iterator.
	 * 
	 * If you define a <i>n</i> dimensional Boolean function just use exactly <i>n</i>
	 * values from the Iterator (call <i>n</i> times <code>next</code>).
	 * 
	 * The behavior is undefined if the Iterator does not provide enough elements.
	 * Don't use the <code>remove</code> method of the Iterator! 
	 * 
	 * @param inputPoint An Iterator over all values of the input point.
	 * 
	 * @return <code>true</code> if the value of the function is 1 and <code>false</code>
	 * otherwise.
	 */
	public abstract boolean getValue(Iterator<Boolean> inputPoint);
	
	/**
	 * Return the dimension of this function.
	 * 
	 * @return The dimension of this function.
	 */
	public abstract int getDimension();
	
	/**
	 * The same as {@link #getValue(Iterator)}, but this time the point is given as an array.
	 * 
	 * @param inputPoint The input point defined as an array.
	 * 
	 * @return <code>true</code> if the value of the function is 1 and <code>false</code>
	 * otherwise.
	 */
	public boolean getValue(final boolean[] inputPoint)
	{
		Iterator<Boolean> it = new Iterator<Boolean>()
			{
				
				int size = inputPoint.length;
				int pos = 0;
				
				@Override
				public boolean hasNext()
				{
					return pos < size;
				}

				@Override
				public Boolean next()
				{
					return inputPoint[pos++];
				}

				@Override
				public void remove()
				{
					// should never happen
					throw new UnsupportedOperationException();
				}
			
			};
		
		return getValue(it);
	}
	
	/**
	 * The same as {@link #getValue(Iterator)}, it simply uses the Iterator of the Iterable
	 * object.
	 * 
	 * @param inputPoint An object that returns an Iterator of type <code>Boolean</code>,
	 * this Iterator is used as the input point.
	 * @return <code>true</code> if the value of the function is 1 and <code>false</code>
	 * otherwise.
	 */
	public boolean getValue(Iterable<Boolean> inputPoint)
	{
		return getValue(inputPoint.iterator());
	}
	
}